package com.young.common.advice;

import com.young.common.execption.ServiceException;
import com.young.common.interf.IBaseController;
import com.young.common.model.ResultData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一异常处理
 */
@Slf4j
@RestControllerAdvice
public class ExceptionAction implements IBaseController {

  @ExceptionHandler({Exception.class})
  public ResultData<?> exception(Exception e) {
    if (e instanceof ServiceException) {
      String errorMsg = ( (ServiceException) e ).getMsg();
      log.error(errorMsg);
      return responseError(errorMsg);
    } else {
      log.error("系统异常！", e);
      return error();
    }
  }

}
